package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.ProjectStatus;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.vo.SysWhitePaperOutlineVo;
import com.ruoyi.system.service.ISysWhitePaperSurveyTaskService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysWhitePaperProjectMapper;
import com.ruoyi.system.domain.SysWhitePaperProject;
import com.ruoyi.system.service.ISysWhitePaperProjectService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 白皮书基本信息Service业务层处理
 *
 * @author ruoyi
 * @date 2025-10-18
 */
@Service
public class SysWhitePaperProjectServiceImpl implements ISysWhitePaperProjectService
{
    @Autowired
    private SysWhitePaperProjectMapper sysWhitePaperProjectMapper;
    @Autowired
    private ISysWhitePaperSurveyTaskService sysWhitePaperSurveyTaskService;

    @Override
    public List<SysUser> countProjectByCreateByList(List<String> createByList) {
        return sysWhitePaperProjectMapper.countProjectByCreateByList(createByList);
    }

    /**
     * 查询白皮书基本信息
     *
     * @param id 白皮书基本信息主键
     * @return 白皮书基本信息
     */
    @Override
    public SysWhitePaperProject selectSysWhitePaperProjectById(Long id)
    {
        return sysWhitePaperProjectMapper.selectSysWhitePaperProjectById(id);
    }

    /**
     * 查询白皮书基本信息列表
     *
     * @param sysWhitePaperProject 白皮书基本信息
     * @return 白皮书基本信息
     */
    @Override
    public List<SysWhitePaperProject> selectSysWhitePaperProjectList(SysWhitePaperProject sysWhitePaperProject)
    {
        List<SysWhitePaperProject> whitePaperProjectList = sysWhitePaperProjectMapper.selectSysWhitePaperProjectList(sysWhitePaperProject);

        if (CollectionUtils.isNotEmpty(whitePaperProjectList)) {
            List<Long> projectIdList = whitePaperProjectList.stream().map(project -> project.getId()).collect(Collectors.toList());

            // 根据项目ID统计采集数据量
            List<Map<String,Object>> collectDataList = sysWhitePaperProjectMapper.countCollectDataNum(projectIdList);

            // 根据项目ID统计有效数据源
            List<Map<String,Object>> dataSourceList = sysWhitePaperProjectMapper.countDataSourceNum(projectIdList);

            for (SysWhitePaperProject whitePaperProject:whitePaperProjectList) {
                Long projectId = whitePaperProject.getId();
                // 匹配采集数据量
                for (Map<String,Object> collectDataMap:collectDataList) {
                    if (projectId.equals((Long)collectDataMap.get("projectId"))) {
                        whitePaperProject.setCollectDataNum((Long) collectDataMap.get("collectDataNum"));
                    }
                }

                // 匹配有效数据源
                for (Map<String,Object> dataSourceMap:dataSourceList) {
                    if (projectId.equals((Long)dataSourceMap.get("projectId"))) {
                        whitePaperProject.setDataSourceNum((Long) dataSourceMap.get("dataSourceNum"));
                    }
                }

                // 计算汇编时长
                Date beginDate = whitePaperProject.getCreateTime();
                if (ProjectStatus.RUN.getCode().equals(whitePaperProject.getProjectStatus())) {// 执行中
                    whitePaperProject.setCollectTime(DateUtils.timeDistance(DateUtils.getNowDate(),beginDate));
                } else if (ProjectStatus.FINISH.getCode().equals(whitePaperProject.getProjectStatus())) {// 已完成
                    whitePaperProject.setCollectTime(DateUtils.timeDistance(whitePaperProject.getUpdateTime(),beginDate));
                }
            }
        }

        return whitePaperProjectList;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void stopSysWhitePaperProject(SysWhitePaperProject sysWhitePaperProject) {
        sysWhitePaperProject.setProjectStatus(ProjectStatus.FINISH.getCode());
        sysWhitePaperProject.setUpdateBy(SecurityUtils.getUsername());
        sysWhitePaperProject.setUpdateTime(DateUtils.getNowDate());
        sysWhitePaperProjectMapper.updateSysWhitePaperProject(sysWhitePaperProject);

        sysWhitePaperSurveyTaskService.updateSurveyTaskStatusByProjectId(sysWhitePaperProject.getId());
    }

    @Override
    public List<SysWhitePaperOutlineVo> selectSysWhitePaperOutlineList(Long projectId) {
        return sysWhitePaperProjectMapper.selectSysWhitePaperOutlineList(projectId);
    }
}
